<?php
/**
 * Print recordset
 *
 * @author Arkadiusz Bisaga <abisaga@telaxus.com>
 * @copyright Copyright &copy; 2006, Telaxus LLC
 * @version 1.0
 * @license MIT
 * @package epesi-utils
 * @subpackage RecordBrowser
 */
if(!isset($_REQUEST['cid']) || !isset($_REQUEST['key'])) die('Invalid usage - missing param');
$cid = $_REQUEST['cid'];
$key = $_REQUEST['key'];
$id = $_REQUEST['id'];

define('CID', $cid);
define('READ_ONLY_SESSION',true);
require_once('../../../include.php');

ModuleManager::load_modules();
set_time_limit(0);

$tcpdf = Libs_TCPDFCommon::new_pdf('L');
Libs_TCPDFCommon::SetFont($tcpdf, Libs_TCPDFCommon::$default_font, '', 13);
Libs_TCPDFCommon::add_page($tcpdf);

$current_user = CRM_Contactscommon::get_my_record();
$list_area = SCTV_AreaCommon::get_area_by_id_manager($current_user['id']);
$room_name = isset($list_area[0]) ? $list_area[0]['name'] : '';

$deliveryToEmployee = Utils_RecordBrowserCommon::get_record('SCTV_DeliveryToEmployee', $id);

$deliveryDate = strtotime($deliveryToEmployee['delivery_date']);
$ngay_thang_nam = 'Ngày ' . date('d', $deliveryDate) . ' tháng ' . date('m', $deliveryDate) . ' năm ' . date('Y', $deliveryDate);

$html = '<table><tr><td align="center">CHI NHÁNH CÔNG TY SCTV TẠI QUẢNG NINH<br>PHÒNG GIAO DỊCH ' . mb_convert_case($room_name,MB_CASE_UPPER,'UTF-8') . '</td><td align="center"><h1>ĐỀ NGHỊ XUẤT KHO</h1><br>Số: ' . $id . '<br>' . $ngay_thang_nam . '</td></tr></table>';
Libs_TCPDFCommon::writeHTML($tcpdf, $html, false);

$html = '<table>';
$html .= '<tr><td>Người đề nghị: ' . CRM_ContactsCommon::contact_format_no_company($deliveryToEmployee['employee']) . '</td></tr>';
$html .= '<tr><td>Bộ phận công tác:</td></tr>';
$html .= '<tr><td>Nội dung: ' . $deliveryToEmployee['note'] . '</td></tr>';
$html .= '</table>';
Libs_TCPDFCommon::writeHTML($tcpdf, $html, false);

$html = '<table width="100%" cellspacing="0" cellpadding="2" border="1" style="font-size:11">';

$html .= '<tr>';
$html .= '<td width="5%" align="center" style="font-weight:bold">STT</td>';
$html .= '<td width="10%" align="center" style="font-weight:bold">Mã vật tư</td>';
$html .= '<td width="25%" align="center" style="font-weight:bold">Tên vật tư</td>';
$html .= '<td width="8%" align="center" style="font-weight:bold">ĐVT</td>';
$html .= '<td width="14%" align="center" style="font-weight:bold">Số lượng xuất</td>';
$html .= '<td width="14%" align="center" style="font-weight:bold">Số lượng</td>';
$html .= '<td width="24%" align="center" style="font-weight:bold">Ghi chú</td>';
$html .= '</tr>';

$deliveryMaterials = Utils_RecordBrowserCommon::get_records('SCTV_DeliveryMaterial', array('delivery'=>$id));
$stt = 0;
foreach($deliveryMaterials as $key=>$deliveryMaterial){
    $stt++;

    $html .= '<tr>';
    $html .= '<td width="5%" align="center">' . $stt . '</td>';

    $material = Utils_RecordBrowserCommon::get_record('SCTV_Material', $deliveryMaterial['material']);
    $html .= '<td width="10%">' . $material['material_code'] . '</td>';
    $html .= '<td width="25%">' . $material['material_name'] . '</td>';
    $html .= '<td width="8%" align="center">' . $material['material_unit'] . '</td>';
    $html .= '<td width="14%"></td>';
    $html .= '<td width="14%"  align="center">' . $deliveryMaterial['amount'] . '</td>';
    $html .= '<td width="24%">' . $deliveryMaterial['note'] . '</td>';
    $html .= '</tr>';
}

$html .= '</table>';
Libs_TCPDFCommon::writeHTML($tcpdf, $html, false);

$html = '<table style="padding-top: 20px;text-align:center;"><tr>';
$html .= '<td>Người đề nghị</td>';
$html .= '<td>Kế toán vật tư</td>';
$html .= '<td>Trưởng phòng</td>';
$html .= '</tr></table>';
Libs_TCPDFCommon::writeHTML($tcpdf, $html, false);

$buffer = Libs_TCPDFCommon::output($tcpdf);

header('Content-Type: application/pdf');
header('Content-Length: '.strlen($buffer));
header('Content-disposition: inline; filename="de_nghi_xuat_kho_'.$id.'.pdf"');

print($buffer);

?>
